package com.hj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hj.reggie.common.BusinessException;
import com.hj.reggie.domain.Dish;
import com.hj.reggie.domain.DishFlavor;
import com.hj.reggie.dto.DishDto;
import com.hj.reggie.mapper.DishMapper;
import com.hj.reggie.service.DishFlavorService;
import com.hj.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 37460
 * 针对表【dish(菜品管理)】的数据库操作Service实现
 * 2023-03-24 14:47:55
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {


    private final DishFlavorService dishFlavorService;

    public DishServiceImpl(DishFlavorService dishFlavorService) {
        this.dishFlavorService = dishFlavorService;
    }

    /**
     * 新增菜品 处理菜品和口味分别入库的方法
     * Transactional 事务注管理事物的多张表的时候进行操作
     *
     * @param dishDto 菜品和口味封装的对象数据
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //获取菜品的id
        Long id = dishDto.getId();
        //遍历集合把菜品id塞进去
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().peek(item -> item.setDishId(id)).collect(Collectors.toList());
        //保存菜品口味数据到dish_flavor；
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdDishWithFlavor(Long id) {
        //创建dishDto对象返回给页面使用
        DishDto dishDto = new DishDto();
        //根据id查询dish菜品
        Dish dish = this.getById(id);
        if (dish == null) {
            throw new BusinessException("当前id不存在");
        }
        //copy dish得属性给dishDto
        BeanUtils.copyProperties(dish, dishDto);
        //根据id查询DishFlavor 菜品口味
        LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
        dishFlavorWrapper.eq(DishFlavor::getDishId, dish.getId());
        return dishDto.setFlavors(dishFlavorService.list(dishFlavorWrapper));
    }

    /**
     * @param dishDto 传入DishDto对象数据
     */
    @Override
    @Transactional(rollbackFor = AbstractMethodError.class)
    public void updateDishWithFlavor(DishDto dishDto) {
        if (dishDto == null) {
            throw new BusinessException("口味不能为空");
        }
        if (dishDto.getFlavors() == null) {
            throw new BusinessException("口味不能为空");
        }
        //更新dish表基本数据
        this.updateById(dishDto);
        //清理当前菜品对应得口味数据
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>()
                //按照dishId进行查询
                .eq(DishFlavor::getDishId, dishDto.getId()));
        //添加当前提交的口味数据  到  dishFlavor表数据
        List<DishFlavor> collect = dishDto.getFlavors().stream().peek(dishFlavor -> dishFlavor.setDishId(dishDto.getId())).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);
    }

    /**
     * 修改菜品售卖的状态
     *
     * @param state 状态 1起售0停售
     * @param ids   修改菜品的id
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void switchSalesStatus(Integer state, Long[] ids) {
        //处理数据格式
        List<Dish> collect = Arrays.stream(ids).map(dishId -> {
            Dish dish = new Dish();
            dish.setStatus(state);
            dish.setId(dishId);
            return dish;
        }).collect(Collectors.toList());
        //按照Id去更新dish菜品的状态
        this.updateBatchById(collect);
    }


}




